package com.qq.googleplay.ui.fragment;

import android.graphics.Color;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.haarman.listviewanimations.swinginadapters.AnimationAdapter;
import com.haarman.listviewanimations.swinginadapters.prepared.AlphaInAnimationAdapter;
import com.qq.googleplay.base.BaseFragment;
import com.qq.googleplay.base.BaseHolder;
import com.qq.googleplay.base.LoadingPager;
import com.qq.googleplay.base.SuperBaseAdapter;
import com.qq.googleplay.bean.AppInfoBean;
import com.qq.googleplay.factory.ListViewFactory;
import com.qq.googleplay.http.Protocol.AppProtocol;
import com.qq.googleplay.ui.holder.AppItemHolder;
import com.qq.googleplay.utils.UIUtils;

import java.util.List;

/**
 * ============================================================
 * 版 权 ： Google互联网有限公司版权所有 (c) 2016
 * 作 者 : 陈冠杰
 * 版 本 ： 1.0
 * 创建日期 ：2016/3/20 19:38
 * 描 述 ：应用页面
 * 修订历史 ：
 * ============================================================
 **/
public class AppFragment extends BaseFragment {

    private List<AppInfoBean> mData;

    private AppProtocol mAppProtocol;

    private AppAdapter mAppAdapter;


    @Override
    protected View initSuccessView() {
        ListView listView = ListViewFactory.createListView();
        listView.setBackgroundColor(Color.parseColor("#EFF4F7"));
        listView.setPadding(0, UIUtils.dip2Px(4),0,0);
        mAppAdapter = new AppAdapter(listView, mData);

        //给listview的item添加渐变动画
        AnimationAdapter animAdapter = new AlphaInAnimationAdapter(mAppAdapter);
        animAdapter.setAbsListView(listView);
        listView.setAdapter(animAdapter);

        // listView.setOnScrollListener();
        //listView.setAdapter(mAppAdapter);
        return listView;
    }

    @Override //加载数据
    protected LoadingPager.LoadedResult initData() {

        mAppProtocol = new AppProtocol();

        try {
            mData = mAppProtocol.loadData(0);
            return checkState(mData);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return LoadingPager.LoadedResult.ERROR;
        }
    }

    class AppAdapter extends SuperBaseAdapter<AppInfoBean> {

        public AppAdapter(AbsListView listView, List<AppInfoBean> dataSource) {
            super(listView, dataSource);
        }

        @Override
        public BaseHolder<AppInfoBean> getSpecialHolder(int position) {
            return new AppItemHolder(mActivity);
        }

        /**
         * @return
         * @throws Throwable
         * @des 加载更多数据
         */
        @Override
        public List<AppInfoBean> onLoadMore() throws Throwable {
            return mAppProtocol.loadData(mData.size());
        }
    }

}
